function [irs,nonStable] = genVMA(phi,opt)
% Given reduced-form VAR parameters, generate coefficients in reduced-form
% vector moving average (VMA) representation and, if required, LRCIR. 
% Also compute the form of these objects used in GK18 (i.e. post-multiplied
% by Sigmatr), and check invertibility of the VAR representation.
% Inputs:
% - phi: structure containing reduced-form VAR parameters
% - opt: structure containing model information and options

Sigmatr = phi.Sigmatr;
B = phi.B;
p = opt.p;
const = opt.const;
H = opt.H;
cumIR = opt.cumIR;
    
N = size(Sigmatr,1); % Number of variables in VAR
B_mat = reshape(B,N*p+const,N); % Reshape into matrix

if const == 1 % If constant included in VAR
    B_mat = B_mat(2:end,:); % Drop constant (does not affect IRs)     
end

% Put VAR(p) in companion form (i.e. VAR(1)).
B_c = zeros(N*p); % Storage
B_c(1:N,:) = B_mat';   
B_c(N+1:end,1:end-N) = eye(N*p-N);

% Compute coefficients in vector moving average representation.
vma = zeros(N,N,H+1); % Storage array 
vmaGK = vma;
CC = eye(N*p);
vma(:,:,1) = CC(1:N,1:N);
vmaGK(:,:,1) = vma(:,:,1)*Sigmatr;

for jj = 2:H+1 % For each horizon

    CC = CC*B_c;
    % Extract upper left NxN block of coefficient matrix in VMA
    % representation of companion form.
    vma(:,:,jj) = CC(1:N,1:N);
    vmaGK(:,:,jj) = vma(:,:,jj)*Sigmatr;
    
end

if opt.calcLRCIR == 1 % Compute long-run IR

    lrcir = (eye(N)-sum(reshape(B_c(1:N,:),[N,N,p]),3))\eye(N);
    lrcirGK = lrcir*Sigmatr;

else
    
    lrcir = [];
    lrcirGK = [];

end

if ~isempty(cumIR)
    
    % Compute cumulative IRF for relevant variables
    vma(cumIR,:,:) = cumsum(vma(cumIR,:,:),3);
    vmaGK(cumIR,:,:) = cumsum(vmaGK(cumIR,:,:),3);
    
end

% Add computed IRs to structure
irs.vma = vma;
irs.vmaGK = vmaGK;
irs.lrcir = lrcir;
irs.lrcirGK = lrcirGK;

% Compute eigenvalues of companion matrix.
E = eig(B_c);
% Check if any eigenvalues are greater than one in modulus.
nonStable = any(abs(E) >= 1);

end